import java.util.HashSet;
import java.util.Set;

public class Test {
    public static void main(String[] args) {
        //
    }
}



 class ListNode {
      int val;
      ListNode next;
      ListNode(int x) {
         val = x;
          next = null;
     }
}
public class Solution {
    public boolean hasCycle(ListNode head) {
        int[] sign = new int[10001];
        ListNode cur = head;
        /*for(int i = 0; i < sign.length; i++){
            if(cur == null){
                break;
            }
            if(sign[i] == 1){
                return true;
            }else{
                sign[i] = 1;
            }
            cur = cur.next;
        }*/
       /* while (cur != null) {
            if (sign[cur.val] == 0) {
                sign[cur.val] = 1;
            }else{
                return true;
            }
            cur = cur.next;
        }*/
        Set<ListNode> settt = new HashSet<ListNode>;
        while (cur != null) {
            if(settt.contains(cur)){
                return true;
            }else{
                settt.add(cur);
                cur = cur.next;
            }
        }
        return false;
    }
}